﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AMatysiak.Methods
{
    public static class StringExtensions
    {        
        public static string FormatWith(this string s, params object[] parameters)
        {
            return String.Format(s, parameters);
        }

        public static bool IsEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        public static bool IsValidEmailAddress(this string s)
        {
            Regex regex = new Regex(@"^[\w-\.]+(@|\[at\])([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(s);
        }

        public static bool IsValidPostalCode(this string s)
        {
            Regex regex = new Regex(@"^[\w]{2}-[\w]{3}$");
            return regex.IsMatch(s);
        }

        public static string Substr(this string s, int length)
        {
            return Substr(s, 0, length);
        }

        public static string Substr(this string s, int start, int length)
        {
            if (length > 0)
                return s.Substring(start, length);
            else if (s.Length >= -length && length <= 0)
                return s.Substring(start, s.Length - start + length);

            throw new ArgumentOutOfRangeException("length", length, 
                "minus length must be equal or less then length of string");
        }

        public static string SubstrLast(this string s, int length)
        {
            return Substr(s, s.Length - length);
        }

        public static string SubstringTo(this string s, string delimiter)
        {
            int index = s.IndexOf(delimiter);
            if (index == -1) return String.Empty;

            return s.Substring(0, index + delimiter.Length);
        }

        public static string SubstringLastTo(this string s, string delimiter)
        {
            if (delimiter.IsEmpty())
                return s;

            int index = s.LastIndexOf(delimiter);
            if (index == -1) return String.Empty;

            return s.Substring(0, index + delimiter.Length);
        }

        public static string SubstringBefore(this string s, string delimiter)
        {
            int index = s.IndexOf(delimiter);
            if (index == -1) return String.Empty;

            return s.Substring(0, index);
        }

        public static string SubstringLastBefore(this string s, string delimiter)
        {
            if (delimiter.IsEmpty())
                return s;

            int index = s.LastIndexOf(delimiter);
            if (index == -1) return String.Empty;

            return s.Substring(0, index);
        }

        public static string SubstringFrom(this string s, string delimiter)
        {
            int index = s.IndexOf(delimiter);
            if (index == -1) return String.Empty;

            return s.Substring(index);
        }

        public static string SubstringLastFrom(this string s, string delimiter)
        {
            if (delimiter.IsEmpty())
                return String.Empty;

            int index = s.LastIndexOf(delimiter);
            if (index == -1) return String.Empty;

            return s.Substring(index);
        }

        public static string SubstringAfter(this string s, string delimiter)
        {
            int index = s.IndexOf(delimiter);
            if (index == -1) return String.Empty;

            return s.Substring(index + delimiter.Length);
        }

        public static string SubstringLastAfter(this string s, string delimiter)
        {
            if (delimiter.IsEmpty())
                return String.Empty;

            int index = s.LastIndexOf(delimiter);
            if (index == -1) return String.Empty;

            return s.Substring(index + delimiter.Length);
        }

        public static int SubstrCount(this string s, string sub)
        {
            string[] tab = s.Split(new string[] { sub }, StringSplitOptions.None);
            return tab.Length - 1;
        }

        public static string GetChars(this string substr, Predicate<char> match)
        {
            StringBuilder builder = new StringBuilder();

            foreach (char c in substr)
                if (match.Invoke(c))
                    builder.Append(c);

            return builder.ToString();
        }
    }
}
